﻿var TweekDay = [

		"Niedziela",
		"Poniedziałek",
		"Wtorek",
		"Środa",
		"Czwartek",
		"Piątek",
		"Sobota"

];

var TMount = [

"Styczeń",
"Luty",
"Marzec",
"Kwiecień",
"Maj",
"Czerwiec",
"Lipiec",
"Sierpień",
"Wrzesień",
"Październik",
"Listopad",
"Grudzień"
];

var TDay = [

//n    p,   w,   s,   c,   p,  s
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]

];


/*
        $.ajax({;
        type: "POST",
        url: @url.Action("GetEvents", "Home"),
        succes: function(date)
        {
            //var events = $.parseJSON(data);
            
            for (var i in data){
                
                console.log(data.name);
            }
        }
        });
    17:00 - 17:29
    1355846437289
    17:30 - 17:59
    
    
 */

var TaskList = [

    [1, "taks name 1", 1355832007089, 1355848237289],
    [1, "taks name 2", 1355836007089, 1355840237289]

];

var i = 0;
var j = 0;
var k = 0;
var l = 0;
var m = 0;

var Adata = new Date();
var data = new Date();

//Adata.setDate(15);
//Adata.setMonth(4);

var aR = (Adata.getFullYear() + 0);
var aM = (Adata.getMonth() + 0);
var aD = (Adata.getDate() + 0);
var aH = (Adata.getHours() + 0);
var aMi = (Adata.getMinutes() + 0);
var aS = (Adata.getSeconds() + 0);

var m1 = aM;
var r1 = aR;
var d1 = aD;
var h1 = aH;
var mi1 = aMi;

var nawigationStatus = 0;
var timep = 30;

function refresLastDay(m2, r2) {
    data.setMonth(m2);
    data.setYear(r2);
    lastDay = 5;
    j = (data.getMonth() + 0);

    for (i = 5; ; i++) {
        data.setDate(i);

        if (j != (data.getMonth() + 0)) {
            break;
        }
        else {
            lastDay = (data.getDate() + 0);;
        }
    }

    return lastDay;
}

function refreshTab() {
    data.setMonth(m1);
    data.setYear(r1);

    data.setDate(1);

    k = (data.getDate() + 0);

    l = (data.getDay() + 0);

    lastDay = refresLastDay(m1, r1);

    for (i = 0; i < 6; i++) {
        for (j = 0; j < 7; j++) {
            TDay[i][j] = "";

            if ((i == 0 && j < l) || lastDay < k) {
                continue;
            }
            else {
                TDay[i][j] = "" + k;
                k++;
            }
        }
    }
}

var data3 = new Date();

function refreshDay() {
    s = "";
    classString = "";

    switch (nawigationStatus) {
        case -1:

            lh = "";
            lm = "";
            k = 0;
            j = 0;

            for (i = 0; ; i++) {
                lm = k * timep;

                if (lm == 60) {
                    j++;
                    k = 0;
                    lm = 0;
                }

                lh = j;

                if (lh > 23) {
                    break;
                }

                if (r1 == aR) {
                    if (m1 == aM) {
                        if (d1 == aD) {
                            if (lh == aH) {
                                if (lm <= aMi && (lm + timep - 1) >= aMi) {
                                    classString = "hoursActual";
                                }
                                else if (lm < aMi) {
                                    classString = "hoursPast";
                                }
                                else {
                                    classString = "hours";
                                }
                            }
                            else if (lh < aH) {
                                classString = "hoursPast";
                            }
                            else {
                                classString = "hours";
                            }
                        }
                        else if (d1 < aD) {
                            classString = "hoursPast";
                        }
                        else {
                            classString = "hours";
                        }
                    }
                    else if (m1 < aM) {
                        classString = "hoursPast";
                    }
                    else {
                        classString = "hours";
                    }
                }
                else if (r1 < aR) {
                    classString = "hoursPast";
                }
                else {
                    classString = "hours";
                }

                lm2 = lm + (timep - 1);

                data3.setDate(d1);
                data3.setMonth(m1);
                data3.setYear(r1);

                data3.setHours(lh);
                data3.setMinutes(lm);

                if (lh < 10) { lh = "0" + lh; }

                if (lm < 10) { lm = "0" + lm; }
                if (lm2 < 10) { lm2 = "0" + lm2; }

                Ttask = "";

                b = 0;
                while (TaskList[b]) {
                    if (TaskList[b][2] < (data3.getTime() + 0) && TaskList[b][3] > (data3.getTime() + 0)) {
                        Ttask += "<div class='taks'>" + TaskList[b][1] + "</div>";
                    }

                    b++;
                }

                s += "<div class='" + classString + "'> " + lh + ":" + lm + " - " + lh + ":" + lm2 + " " + (data3.getTime() + 0) + " <div>" + Ttask + "</div></div>";

                k++;

                /*
                    j = 0;
                    k = 0;
                    
                    while(TaskList[j])
                    {
                        if(TaskList[j][0] == d1 && TaskList[j][1] == (m1+1) && TaskList[j][2] == r1)
                        {
                            s += "<div class='hours'> " + TaskList[j][3] + " </div>";
                            k = 1;
                        }
                        j++;
                    }
                    
                    if(k == 0)
                    {
                        s = "<div class='hours'> brak zadan _____ " + d1 + "." + m1 + "." + r1 + " </div>";
                    }
                */
            }

            break;

        case 0:

            for (i = 0; i < 6; i++) {
                for (j = 0; j < 7; j++) {
                    if (TDay[i][j] != "" && TDay[i][j] != " ") {
                        if (r1 == aR) {
                            if (m1 == aM) {
                                if (TDay[i][j] == aD) {
                                    classString = "daysActual";
                                }
                                else if (TDay[i][j] < aD) {
                                    classString = "daysPast";
                                }
                                else {
                                    classString = "days";
                                }
                            }
                            else if (m1 < aM) {
                                classString = "daysPast";
                            }
                            else {
                                classString = "days";
                            }
                        }
                        else if (r1 < aR) {
                            classString = "daysPast";
                        }
                        else {
                            classString = "days";
                        }
                    }
                    else {
                        classString = "days";
                    }

                    s += "<div id='a" + i + "' onclick='statusPlus(" + TDay[i][j] + ")' class='" + classString + "'>" + TDay[i][j] + "</div>";
                }
            }

            break;

        case 1:

            for (i = 0; i < 12; i++) {
                if (r1 == aR) {
                    if (i == aM) {
                        classString = "mounthsActual";
                    }
                    else if (i < aM) {
                        classString = "mounthsPast";
                    }
                    else {
                        classString = "mounths";
                    }
                }
                else if (r1 < aR) {
                    classString = "mounthsPast";
                }
                else {
                    classString = "mounths";
                }

                s += "<div id='a" + i + "' onclick='statusPlus(" + i + ")' class='" + classString + "'>" + TMount[i] + "</div>";
            }

            break;

        case 2:

            for (i = r1 - 50; i < r1 + 49; i++) {
                if (i == aR) {
                    classString = "yearsActual";
                }
                else if (i < aR) {
                    classString = "yearsPast";
                }
                else {
                    classString = "years";
                }

                s += "<div id='a" + i + "' onclick='statusPlus(" + i + ")' class='" + classString + "'>" + i + "</div>";
            }

            break;
    }

    $("div#CalendarDay").html(s);
}

function refresWeekDay() {
    s = "";

    switch (nawigationStatus) {
        case 0:

            for (i = 0; i < 7; i++) {
                s += "<div id='d" + i + "' class='day'><div class='day2'><center>" + TweekDay[i] + "</center></div></div>";
            }

            break;
    }

    $("div#CalendarWeekDay").html(s);
}
//doksyzwn

function refresActualTime() {
    s = "";

    aH2 = aH;
    aMi2 = aMi;
    aS2 = aS;

    if (aH < 10) { aH2 = "0" + aH; }
    if (aMi < 10) { aMi2 = "0" + aMi; }
    if (aS < 10) { aS2 = "0" + aS; }

    s += "<div id='actual'><center><span style='font-size: 20px;'>" + aH2 + ":" + aMi2 + ":" + aS2 + "</span> " + aD + " " + TMount[aM] + " " + aR + "</center></div>";

    $("div#CalendarNaw1").html(s);
    $("div#actual").click(function () {
        nawigationStatus = 0;

        r1 = aR;
        m1 = aM;
        d1 = aD;

        refreshTab();
        nawA();
        refresWeekDay();
        refreshDay();

    });
}

function refreshHtml() {
    s = "";

    s += "<div id='nawigacja1' >Wcześniej</div><div id='nawigacja2' >Później</div><div id='nawA'>.....</div>";

    $("div#CalendarNaw2").html(s);
    $("div#nawigacja1").click(function () {

        switch (nawigationStatus) {
            case -1:

                d1--;

                if (d1 <= 0) {
                    m1--;

                    if (m1 < 0) {
                        m1 = 11;
                        r1--;
                    }

                    d1 = refresLastDay(m1, r1);
                }

                break;
            case 0:

                m1--;

                if (m1 < 0) {
                    m1 = 11;
                    r1--;
                }

                break;
            case 1:

                r1--;

                break;
            case 2:

                r1 -= 99;

                break;
        }

        refreshTab();
        nawA();
        refreshDay();

    });

    $("div#nawigacja2").click(function () {

        switch (nawigationStatus) {
            case -1:

                d1++;

                if (d1 > refresLastDay(m1, r1)) {
                    m1++;

                    if (m1 > 11) {
                        m1 = 0;
                        r1++;
                    }

                    d1 = 1;
                }

                break;
            case 0:

                m1++;

                if (m1 > 11) {
                    m1 = 0;
                    r1++;
                }

                break;
            case 1:

                r1++;

                break;
            case 2:

                r1 += 99;

                break;
        }

        refreshTab();
        nawA();
        refreshDay();

    });
}

var tabGrat = [2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60];

function nawA() {
    s = "";

    switch (nawigationStatus) {
        case -1:

            s = "<div><center>" + d1 + " " + TMount[m1] + " " + r1 + " </center></div>";


            s += "<div id='grat'>";


            i = 0;
            while (tabGrat[i]) {

                if (tabGrat[i] == timep) {
                    s += "<div class='grat' onclick='fgranulation(" + tabGrat[i] + ")'><b>" + tabGrat[i] + "</b></div>";
                }
                else {
                    s += "<div class='grat' onclick='fgranulation(" + tabGrat[i] + ")'>" + tabGrat[i] + "</div>";
                }
                i++;
            }

            s += "<div style='visibility: hidden; font-size: 1px; width: 1px;'>.</div>";


            s += "</div>";


            break;
        case 0:

            s = "<center> " + TMount[m1] + " " + r1 + " </center>";

            break;
        case 1:

            s = "<center> " + r1 + " </center>";

            break;
        case 2:

            s = "<center>" + (r1 - 50) + "-" + (r1 + 48) + "</center>";

            break;
    }

    $("div#nawA").html(s);
}

function fgranulation(m) {
    timep = m;

    refreshTab();
    refresActualTime();
    refreshHtml();
    nawA();
    refreshDay();

}

refreshTab();
refresActualTime();
refreshHtml();
nawA();
refresWeekDay();
refreshDay();

function statusPlus(s1) {
    switch (nawigationStatus) {
        case 0:

            if (s1 != undefined) {
                nawigationStatus--;
                d1 = s1;
            }

            break;
        case 1:

            nawigationStatus--;
            m1 = s1;

            break;
        case 2:

            nawigationStatus--;
            r1 = s1;

            break;
    }

    refreshTab();
    nawA();
    refresWeekDay();
    refreshDay();
}

function statusMinus() {
    if (nawigationStatus < 2) {
        nawigationStatus++;
        nawA();
        refresWeekDay();
        refreshDay();
    }
}

$("div#nawA").click(function () {

    statusMinus();

});

zmien();
function zmien() {
    datazmien();
    refresActualTime();

    refreshTab();
    nawA();
    refresWeekDay();
    refreshDay();

    setTimeout("zmien();", 1000);
}

function datazmien() {
    Adata = new Date();

    aR = (Adata.getFullYear() + 0);
    aM = (Adata.getMonth() + 0);
    aD = (Adata.getDate() + 0);
    aH = (Adata.getHours() + 0);
    aMi = (Adata.getMinutes() + 0);
    aS = (Adata.getSeconds() + 0);
}